package com.joysuch.wwyt.datadump.biz.processor;

import cn.zlg.common.datadump.annotation.DataDumpEntity;
import cn.zlg.common.datadump.processor.BaseImportEntityProcessor;
import com.joysuch.wwyt.api.dataaccess.OrientationDataSyncService;
import com.joysuch.wwyt.api.dataaccess.bean.OrientationSearchBean;
import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.datadump.biz.model.BpJobDumpBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * @author zero
 * @since 2022/1/6 10:45
 */
@Component
public class BpJobImportProcessor extends BaseImportEntityProcessor<BpJobDumpBean, BpJob> {

    @Autowired
    private OrientationDataSyncService orientationDataSyncService;
    @Autowired
    private BaseConfigService baseConfigService;

    @Override
    public BpJob processBeforePersist(BpJobDumpBean origin, BpJob entity, DataDumpEntity annotation, boolean override) {
        if (StringUtils.isBlank(origin.getAlarmTemplateName())) {
            entity.setAlarmTemplateId(null);
            entity.setAlarmTemplateName(null);
            return entity;
        }
        OrientationSearchBean searchBean = new OrientationSearchBean();
        searchBean.setTemplateName(entity.getAlarmTemplateName());
        searchBean.setPage(0);
        searchBean.setSize(15);
        ResultBean resultBean = null;
        try {
            resultBean = orientationDataSyncService.alarmTemplate(searchBean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("报警模板查询失败");
        }
        if (null != resultBean) {
            if (null != resultBean.getData()) {
                Object data = resultBean.getData();
                List<Map<String, Object>> dateList = (List<Map<String, Object>>) data;
                Integer alarmTemplateId = null;
                for (Map<String, Object> map : dateList) {
                    String alarmTemplateName = (String) map.get("name");
                    if (entity.getAlarmTemplateName().equals(alarmTemplateName)) {
                        Map<String, Object> dateMap = dateList.get(0);
                        alarmTemplateId = (Integer) dateMap.get("id");
                        entity.setAlarmTemplateId(Long.valueOf(alarmTemplateId));
                        break;
                    }
                }
                if (null == alarmTemplateId) {
                    throw new IllegalArgumentException("报警模板不存在，请检查确认");
                }
            } else {
                throw new IllegalArgumentException("报警模板不存在，请检查确认");
            }
        } else {
            throw new IllegalArgumentException("报警模板查询失败");
        }
        return entity;
    }

    private Boolean getAlarmSwitch() {
        BaseConfig config = baseConfigService.findConfig(Context.getCurrentTenantId(), Context.getCompanyCode(), BaseConfigCodes.LOCATE_SERVICE_ALARM_OPEN);
        return config == null || StringUtils.isBlank(config.getValue()) || config.getValue().trim().equals("true");
    }
}
